﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WCity : NotificationObject
    {
        private int cityUID;
        private String cityName;
        private int personnelUID;
        private int provinceUID;
        private WPersonnel person;

        [DataMember(Order = 0)]
        public int CityUID
        {
            get
            {
                return cityUID;
            }
            set
            {
                cityUID = value;
                RaisePropertyChanged("CityUID");
            }
        }

        [DataMember(Order = 1)]
        public String CityName
        {
            get
            {
                return cityName;
            }
            set
            {
                cityName = value;
                RaisePropertyChanged("CityName");
            }
        }

        [DataMember(Order = 2)]
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        [DataMember(Order = 3)]
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }
            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        [DataMember(Order = 4)]
        public WPersonnel Person
        {
            get
            {
                return person;
            }
            set
            {
                person = value;
                RaisePropertyChanged("Person");
            }
        }

        public WCity()
        {
            CityUID = -1;
            CityName = "";
            PersonnelUID = -1;
            ProvinceUID = -1;
            Person = new WPersonnel();
        }

        public WCity(LCity p)
        {
            CityUID = p.CityUID;
            CityName = p.CityName;
            PersonnelUID = p.PersonnelUID;
            ProvinceUID = WProvince.GetProvinceUID(p.ProvinceSelected);
        }

        public static int GetCityUID(String pCityName)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.BD_City, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return -1;
            }

            List<WCity> list = JSON.parse<List<WCity>>(res.Data1);
            foreach (var item in list)
            {
                if (item.CityName.Trim() == pCityName.Trim())
                {
                    return item.CityUID;
                }
            }

            return -1;
        }

        public static List<WCity> GetAll()
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.BD_City, EMethod.SelectAll, "");
            return JSON.parse<List<WCity>>(res.Data1);
        }

        public static HttpResponse Insert(List<WCity> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_City, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WCity> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_City, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WCity> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_City, EMethod.Delete, postData);
        }
    }
}
